package cn.com.generaldata.os.service.common;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.paging.Event;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.paging.Page;
import cn.com.generaldata.os.paging.Result;
import cn.com.generaldata.os.pojo.AreaPerform;
import cn.com.generaldata.os.pojo.LArea;

@Component
public class LAreaService {

	private static Logger log = LoggerFactory.getLogger(LAreaService.class);
	
	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<LArea> areaDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#createArea(cn.com.generaldata.os.pojo.Area)
	 */

	public String createLArea(LArea area) {
		if (area != null) {
			if (StringUtils.isNotBlank(area.getLareaName())) {
				LArea oldArea = checkLAreaName(area.getLareaName());
				if (oldArea != null) {
					// throw new CommonException(
					// ExceptionConstant.CODE_DUPLICATE_ROLE_NAME,
					// ExceptionConstant.CAUSE_CREATE_ROLE);
					log.error("area为空");
					return null;
				}
			}
			if (area.getId() == null || "".equals(area.getId()))
				area.setId(UUID.randomUUID().toString().replaceAll("-", ""));

			areaDAO.save(area);
			return area.getId();
		}
		return null;
	}
	
	//只查询直属下级
	public List<LArea> getLAreaChildren(String areaId) {
		List<LArea> list = new ArrayList<LArea>();
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "parentId", areaId));
		List<LArea> lareas = areaDAO.find(LArea.class, filter, null);
		for (LArea obj : lareas){
			obj.setUnits(null);
			obj.setRegistUnits(null);
			list.add(obj);
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#modifyArea(cn.com.generaldata.os.pojo.Area)
	 */

	public LArea modifyLArea(LArea area) {
		if (area != null) {
			areaDAO.update(area);
			return area;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#queryAreaById(java.lang.String)
	 */

	public LArea queryLAreaById(String id) {
		if (id != null) {
			return areaDAO.get(LArea.class, id);
		}
		return null;
	}


	public Result queryLAreas(Filter filter, Page page) {
		List<LArea> areas = areaDAO.find(LArea.class, filter, page);
		int count = areaDAO.count(LArea.class, filter);

		if (page == null) {
			return new Result(1, 1, count, areas, new ArrayList<Event>());
		}
		return new Result(page.getPageNumber(), page.getPageSize(), count, areas, new ArrayList<Event>());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#removeAreas(java.lang.String[])
	 */

	public void removeLAreas(String[] ids) {
		if (ids != null && ids.length > 0) {

			Filter filter = new Filter();
			filter.addRule(new FilterRule(FilterConstant.COLLECTION, "id", ids));

			List<LArea> areas = areaDAO.find(LArea.class, filter, null);
			areaDAO.delete(areas);
		}
	}

	private LArea checkLAreaName(String name) {
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "lareaName", name));

		List<LArea> areas = areaDAO.find(LArea.class, filter, null);
		if (areas.size() > 0) {
			return areas.get(0);
		}
		return null;
	}

}
